﻿
using IHISCETeim.BLL;
using IHISCETeim.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using IHISCETeim.Library;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class AddAgentQlty2 : AgentPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.新增 };
            }
        }
        protected override string DWZRef
        {
            get { return "AddAgentQlty"; }
        }

        #region 初始化数据
        protected void InitLoad()
        { 
            //初始货配件数据
            DataTable table = new DataTable();
            table.Columns.Add(new DataColumn("Id", typeof(int)));
            for (int rowindex = 1; rowindex <= 20; rowindex++)
            {
                DataRow row = table.NewRow();
                row[0] = rowindex;
                table.Rows.Add(row);
            }

            this.rpParts.DataSource = table;
            this.rpParts.DataBind();

            //初始化图片数量
            DataTable imgdt = new DataTable();
            imgdt.Columns.Add(new DataColumn("Id", typeof(int)));
            imgdt.Columns.Add(new DataColumn("ImgTitle", typeof(string)));
            for (int rowindex = 1; rowindex <= 6; rowindex++)
            {
                DataRow row = imgdt.NewRow();
                row[0] = rowindex;
                string imgtitlestr = "";
                switch (rowindex)
                { 
                    case 1:
                        imgtitlestr = "铭牌";
                        break;
                    case 2:
                        imgtitlestr = "工作小时";
                        break;
                    case 3:
                    case 4:
                    case 5:
                        imgtitlestr = "故障图片";
                        break;
                    case 6:
                        imgtitlestr = "整机及工况图片";
                        break;
                }
                row[1] = imgtitlestr;
                imgdt.Rows.Add(row);
            }

            this.rpImgs.DataSource = imgdt;
            this.rpImgs.DataBind();
        }

        protected string GetMyAgent()
        {
            DataTable dt= OUserController.GetPagination("", "", 2, 1, 9999999, "UserId desc", ref TotalCount);
            DataRow[] dttemp=  dt.Select("SrvActUserId=" + base.User.UserId.ToString());
            if (dttemp.Count() <= 0)
                return "";
            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in dttemp)
            {
                sb.AppendFormat("<option value='{0}'>{1}</option>", dr["UserId"], dr["Department"]);
            }
            return sb.ToString();
        }
        protected string GetSvrInfo()
        {
            OUserInfo svrUser = OUserController.GetById(base.User.SrvActUserId);
            return string.Format("{0} {1}", svrUser.RealName, svrUser.Mobile);
        }
        private DataTable _lastPartsAcceptDt = null;
        protected string GetLastPartsAccept(string columnName)
        {
            if (_lastPartsAcceptDt == null)
                _lastPartsAcceptDt = AgentQltyController.GetLastPartsAcceptInfo(base.User.UserId);
            if (_lastPartsAcceptDt.Rows.Count > 0)
                return _lastPartsAcceptDt.Rows[0][columnName].ToString();
            return "";
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                add();

            }

            InitLoad();
        }

        private void add()
        {
            AgentQltyInfo obj = Request.SerializableObject<AgentQltyInfo>();
            obj.ClaimsNumber = "";
            obj.IsVisit = false;
            obj.SvrResult = "";
            obj.SvrScheme = "";
            obj.PartsSendDate = "";
            obj.PartsSendType = "";
            //经销商
            if (base.User.UserType == 2)
            {
                obj.UserId = this.User.UserId;
                obj.AgentName = this.User.Department;
                obj.SrvActUserId = this.User.SrvActUserId;
                obj.SrvActRealName = OUserController.GetById(User.SrvActUserId).RealName;
            }
            if (base.User.UserType == 1)//服务技术            
            {
                obj.UserId = GetRequestValue<int>("AgentUserId",0);
                OUserInfo objUser = OUserController.GetById(obj.UserId);
                obj.AgentName = objUser.Department;
                obj.SrvActUserId = objUser.SrvActUserId;
                obj.SrvActRealName = OUserController.GetById(objUser.SrvActUserId).RealName;
            }

            //obj.SrvMangerUserId = AgentConfigController.SvrManagerUserId;
            obj.ClaimState = ClaimState.担当确认中.ToString();
            obj.RejectReason = "";
            obj.CancelReason = "";
            obj.AgentSvrTel = "";
            obj.AgentSvrUserId = 0;

            obj.CCPartEmail = "";
            char[] caSplit = new char[] { ',' };
            //加载图片信息
            AgentQltyImgInfo imgObj = new AgentQltyImgInfo();
            List<AgentQltyImgInfo> imgList = new List<AgentQltyImgInfo>();
            string[] imgRelativelyS = GetRequestValue<string>("imgRelatively", "").Split(caSplit);
            string[] ImgTitleS = GetRequestValue<string>("ImgTitle", "").Split(caSplit);
            for (int index = 0; index < imgRelativelyS.Count(); index++)
            {
               
                    imgObj = new AgentQltyImgInfo();
                    imgObj.Title = ImgTitleS[index];
                    imgObj.IMGRelatively = imgRelativelyS[index];
                    imgObj.FileIndex = index+1;
                    imgObj.FromType = 0;
                    imgList.Add(imgObj);
               
            }           

           //加载配件信息
            List<AgentQltyPartsInfo> partsList = new List<AgentQltyPartsInfo>();
            string[] ItemCodes = GetRequestValue<string>("ItemCode", "").Split(caSplit);
            string[] Dscriptions = GetRequestValue<string>("Dscription", "").Split(caSplit);
            string[] Quantitys = GetRequestValue<string>("Quantity", "").Split(caSplit);
            string[] PartsDemands = GetRequestValue<string>("PartsDemand", "").Split(caSplit);
            for (int index = 0; index < ItemCodes.Count(); index++)
            {
                if (ItemCodes[index] != "")
                {
                    AgentQltyPartsInfo pobj = new AgentQltyPartsInfo();
                    pobj.ItemCode = ItemCodes[index];
                    pobj.Dscription = Dscriptions[index];
                    pobj.Quantity = Convert.ToDecimal(Quantitys[index]);
                    pobj.PartsDemand = PartsDemands[index];;
                    pobj.LastPurPrice = Convert.ToDecimal(SBOController.GetOITMLastPurPrice(pobj.ItemCode));
                    pobj.InStock = true;//默认需要返回
                    partsList.Add(pobj);
                }
            }

            if (AgentQltyController.Add(obj, imgList, partsList) > 0)
            {
                SuccessJson(String.Format("提交成功<br/>您的品情已提交到服务担当{0}进行处理.",obj.SrvActRealName) , "AgentQltyList");
            }
            else
            {
                ErrorJson("添加品情表失败！");
            }

          //  SuccessJson("提交成功<br/>" + XX, "AgentQltyList");
        }
    }
}